package com.yango.teach.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.microsoft.schemas.office.office.STInsetMode;
import com.yango.commonutil.result.Result;
import com.yango.teach.entity.Course;
import com.yango.teach.entity.vo.course.CourseInfoForm;
import com.yango.teach.entity.vo.course.CoursePublishVo;
import com.yango.teach.entity.vo.course.CourseQuery;
import com.yango.teach.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author LiDingYin
 * @since 2021-07-24
 */
@Api(description = "课程 前端控制器")
@RestController
@RequestMapping("/teach/course")
public class CourseController {

    @Autowired
    private CourseService courseService;


    //保存课程基本信息-返回课程id
    @ApiOperation(value = "保存课程基本信息-返回课程id")
    @PostMapping("/saveCourseInfo")
    public Result saveCourseInfo(@ApiParam(name = "courseInfoForm", value = "课程信息对象", required = true)
                                 @RequestBody CourseInfoForm courseInfoForm) {
        String courseId = courseService.saveCourseInfo(courseInfoForm);
        if (courseId != null) {
            return Result.ok().message("课程基本信息添加成功!").data("courseId", courseId);
        } else {
            return Result.error().message("课程基本信息保存失败!");
        }
    }

    //根据课程id获取课程基本信息
    @ApiOperation(value = "根据课程id获取课程基本信息")
    @GetMapping("/getCourseInfoById/{courseId}")
    public Result getCourseInfoById(@ApiParam(name = "courseId", value = "课程id", required = true)
                                    @PathVariable String courseId) {
        CourseInfoForm courseInfoForm = courseService.getCourseInfoById(courseId);
        return Result.ok().data("courseInfoForm", courseInfoForm);
    }

    //更新课程基本信息
    @ApiOperation(value = "更新课程基本信息")
    @PutMapping("/updateCourse/{courseId}")
    public Result updateCourse(@ApiParam(name = "courseId", value = "课程id", required = true)
                               @PathVariable String courseId,
                               @ApiParam(name = "courseInfoForm", value = "课程基本信息对象", required = true)
                               @RequestBody CourseInfoForm courseInfoForm) {
        //System.out.println("===================" + courseId);
        //设置课程id
        courseInfoForm.setId(courseId);
        boolean flag = courseService.updateCourse(courseInfoForm);
        if (flag) {
            return Result.ok().message("更新课程基本信息成功!");
        } else {
            return Result.error().message("更新课程接班信息失败!");
        }
    }

    //查询课程确认信息
    @ApiOperation(value = "课程确认信息")
    @GetMapping("/getCoursePublish/{courseId}")
    public Result getCoursePublish(@ApiParam(name = "courseId", value = "课程id", required = true)
                                   @PathVariable String courseId) {
        CoursePublishVo coursePublishVo = courseService.getCoursePublish(courseId);
        if (coursePublishVo != null) {
            return Result.ok().data("coursePublishVo", coursePublishVo);
        } else {
            return Result.error().message("没有查询到课程信息!");
        }
    }

    //发布课程-修改课程状态为Normal
    @ApiOperation(value = "发布课程-修改课程状态为Normal")
    @PutMapping("/publishCourse/{courseId}")
    public Result publishCourse(@ApiParam(name = "courseId", value = "课程id", required = true)
                                @PathVariable String courseId) {
        boolean update = courseService.publishCourse(courseId);
        if (update) {
            return Result.ok().message("课程发布成功!");
        } else {
            return Result.error().message("课程发布失败!");
        }
    }

    //课程列表-条件查询带分页
    @ApiOperation(value = "课程列表-条件查询带分页")
    @PostMapping("/pageCourseList/{current}/{limit}")
    public Result pageCourseList(@ApiParam(name = "current", value = "当前页", required = true)
                                 @PathVariable long current,
                                 @ApiParam(name = "limit", value = "每页显示记录数", required = true)
                                 @PathVariable long limit,
                                 @ApiParam(name = "courseQuery", value = "查询对象", required = false)
                                 @RequestBody CourseQuery courseQuery) {
        //创建page对象
        Page<Course> pageParam = new Page<>(current, limit);
        //调用方法查询
        courseService.pageCourseList(pageParam, courseQuery);
        //获取总记录数
        List<Course> records = pageParam.getRecords();
        //获取总记录数
        long total = pageParam.getTotal();
        return Result.ok().data("total", total).data("pageList", records);
    }

    //删除课程
    @ApiOperation(value = "删除课程")
    @DeleteMapping("/removeCourse/{courseId}")
    public Result removeCourse(@ApiParam(name = "courseId", value = "课程id", required = true)
                               @PathVariable String courseId) {
        boolean flag = courseService.removeCourse(courseId);
        if (flag) {
            return Result.ok().message("删除课程成功!");
        } else {
            return Result.error().message("删除课程失败!");
        }
    }

    //统计每天新增课程数
    @ApiOperation(value = "统计每天新增课程数")
    @GetMapping("/getSaveCourseCount/{day}")
    public Result getSaveCourseCount(@ApiParam(name = "day", value = "统计日期", required = true)
                                     @PathVariable String day) {
        Integer count = courseService.getSaveCourseCount(day);
        return Result.ok().data("courseCount", count);
    }

}

